package dynamic_handle;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import DB.DB_connection_pool;


public class leaving_messages extends HttpServlet {
	private static final long serialVersionUID = 1L;


	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		PrintWriter out = response.getWriter();
		String user_account = request.getSession().getAttribute("user_account").toString();
		
		String send_user = request.getParameter("send_user");
		if(send_user==null||send_user.equals("")){       //请求所有的留言
		String select_leaving = "select * from leaving_messages where receive_leaving_messages = '"+user_account+"'";
		JSONObject jsonObj_r = new JSONObject();
		JSONArray my_leaving_Array = new JSONArray();
		
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select_leaving = conn.prepareStatement(select_leaving);
			ResultSet rs_leaving = ps_select_leaving.executeQuery();
			
			while(rs_leaving.next()){
			  JSONObject jsonObj = new JSONObject();
		      String select_send_user = "select * from user_data where user_account = '"+rs_leaving.getString("send_leaving_messages")+"'";
		      PreparedStatement ps_select_send_user = conn.prepareStatement(select_send_user);
			  ResultSet rs_send_user = ps_select_send_user.executeQuery();
		      if(rs_send_user.next()){
		    	    jsonObj.put("user_account", rs_send_user.getString("user_account"));                        //用户账号
					jsonObj.put("user_nickname", rs_send_user.getString("user_nickname"));                    //用户昵称
					jsonObj.put("user_photo_path", rs_send_user.getString("user_photo_path"));                //用户头像URL
		      }
		      jsonObj.put("leaving_content", rs_leaving.getString("leaving_content"));
		      jsonObj.put("leaving_date", rs_leaving.getDate("leaving_date"));
		      jsonObj.put("leaving_id", rs_leaving.getString("leaving_id"));
		      
		      my_leaving_Array.put(jsonObj);
		     
		      ps_select_send_user.close();
		      rs_send_user.close();
			}
			jsonObj_r.put("result", true);
			jsonObj_r.put("my_leaving_Array", my_leaving_Array);
			
			rs_leaving.close();
			ps_select_leaving.close();
			conn.close();
			
		} catch (SQLException e) {
			jsonObj_r.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jsonObj_r.toString());
	}else{       //请求某一个人的留言
		String select_leaving = "select * from leaving_messages where receive_leaving_messages = '"+user_account+"' and send_leaving_messages = '"+send_user+"'";
		
		JSONArray my_leaving_Array = new JSONArray();
		JSONObject jsonObj_r = new JSONObject();
		try {
			
			Connection conn = DB_connection_pool.data_pool.getConnection();
			
			String select_send_user = "select * from user_data where user_account = '"+send_user+"'";
			 PreparedStatement ps_select_send_user = conn.prepareStatement(select_send_user);
			 ResultSet rs_send_user = ps_select_send_user.executeQuery();
			 String send_user_account = "";
			 String send_user_nickname = "";
			 String send_user_photo_path = "";
			if(rs_send_user.next()){
				send_user_account = rs_send_user.getString("user_account");                      //用户账号
				send_user_nickname = rs_send_user.getString("user_nickname");                  //用户昵称
				send_user_photo_path = rs_send_user.getString("user_photo_path");              //用户头像URL
	      }
			rs_send_user.close();
			ps_select_send_user.close();
			
			PreparedStatement ps_select_leaving = conn.prepareStatement(select_leaving);
			ResultSet rs_leaving = ps_select_leaving.executeQuery();
			while(rs_leaving.next()){
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("user_account", send_user_account);
				jsonObj.put("user_nickname", send_user_nickname);
				jsonObj.put("user_photo_path", send_user_photo_path);
				jsonObj.put("leaving_content", rs_leaving.getString("leaving_content"));
				jsonObj.put("leaving_date", rs_leaving.getDate("leaving_date"));
				jsonObj.put("leaving_id", rs_leaving.getDate("leaving_id"));
				
				my_leaving_Array.put(jsonObj);
			}
			rs_leaving.close();
			ps_select_leaving.close();
			conn.close();
			
			jsonObj_r.put("result", true);
			jsonObj_r.put("my_leaving_Array", my_leaving_Array);
		} catch (SQLException e) {
			jsonObj_r.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		out.print(jsonObj_r.toString());
		
	}
		
	}

}
